package com.application.DataProvider;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.application.DTO.UsingQuestionListDTO;

public class UsingQuestionListDAO {
	Context _mcontext = null;
	public UsingQuestionListDAO(Context context){
		this._mcontext = context;
	}
	public boolean isExist(int idDetailLearning, int idQuestion)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from UsingQuestionList where IDQuestion=" +  idQuestion + " and IDDetailLearning="+ idDetailLearning;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}
	public ArrayList<UsingQuestionListDTO> getUsingQuestionList_idDetailLearning(int idDetailLearning)
	{
		ArrayList<UsingQuestionListDTO> uqlList = new ArrayList<UsingQuestionListDTO>();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from UsingQuestionList where idDetailLearning = " + idDetailLearning;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				UsingQuestionListDTO uqlDto = new UsingQuestionListDTO();
				uqlDto.set_IDQuestion(cur.getInt(1));
				uqlDto.set_IDDetailLearning(cur.getInt(0));
				uqlList.add(uqlDto);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return uqlList;
	}
	
	
	public int insert(UsingQuestionListDTO uqlDto) {
		Log.d("PEERDROID","insert FeelChoice");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("IDQuestion", uqlDto.get_IDQuestion());
			values.put("IDDetailLearning", uqlDto.get_IDDetailLearning());
			
			result = provider.execInsertCommand(db, "UsingQuestionList", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
	
	public int insert(int idQuestion, int idDetailLearning) {
		Log.d("PEERDROID","insert FeelChoice");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("IDQuestion", idQuestion);
			values.put("IDDetailLearning", idDetailLearning);
			
			result = provider.execInsertCommand(db, "UsingQuestionList", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
	
	public int delete(int idQuestion, int idDetailLearning)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="IDQuestion=? and IDDetailLearning=?";
			String[]whereArgs = {String.valueOf(idQuestion), String.valueOf(idDetailLearning)};	
			result=provider.execDeleteCommand(db, "UsingQuestionList", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
}
